package com.zhongkai.web.control.config;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.model.book.TXtXxts;
import com.zhongkai.model.config.User;
import com.zhongkai.service.CommonService;
import com.zhongkai.service.book.ReadMessageService;
import com.zhongkai.service.config.ActionService;
import com.zhongkai.service.config.DepartmentService;
import com.zhongkai.service.config.RoleService;
import com.zhongkai.service.config.UserService;
import com.zhongkai.tools.CheckWord;
import com.zhongkai.tools.StringTool;

@Controller
public class LoginController {
	private Logger log=Logger.getLogger(this.getClass());
	private UserService userService;
	private DepartmentService departmentService;
	private RoleService roleService;
	private ActionService actionService;

	public DepartmentService getDepartmentService() {
		return departmentService;
	}
	
	@Resource
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}


	public UserService getUserService() {
		return userService;
	}
	
	
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	
	public RoleService getRoleService() {
		return roleService;
	}
	
	@Resource
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	@Resource
	public void setActionService(ActionService actionService) {
		this.actionService = actionService;
	}

	/*登录*/
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/login.do")
	public String login(User user,HttpServletRequest request,HttpServletResponse response) throws Exception {
		String code=request.getParameter("code");
		if(code==null || code.equals("")){
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("请输入验证码!", "/login.jsp",request));
			return null;
		}
		String systemLoginCode=request.getSession().getAttribute("systemLoginCode")==null?"":request.getSession().getAttribute("systemLoginCode").toString();
		if(!code.equals(systemLoginCode)){
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("验证码输入错误!", "/login.jsp",request));
			return null;
		}
		String userAccount=user.getUserAccount();
		if(userAccount==null || userAccount.equals("")){
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("用户名不能为空!", "/login.jsp",request));
			return null;
		}
		String userPassword=user.getUserPassword();
		if(userPassword==null || userPassword.equals("")){
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("密码不能为空!", "/login.jsp",request));
			return null;
		}
		userPassword=StringTool.encrypt(userPassword);
		List checkUser=userService.findByHql("from User where userAccount=? and userPassword=?", new Object[]{userAccount,userPassword});
		if(checkUser==null || checkUser.isEmpty() || checkUser.size()==0){
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("用户名或者密码错误!", "/login.jsp",request));
			return null;
		}
		else{
			User currentUser=(User)checkUser.get(0);
			if(currentUser.getUserSwitch().shortValue()==0){
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("该帐号已被禁用!", "/login.jsp",request));
				return null;
			}
			else{
				request.getSession().setAttribute("user",currentUser);
				actionService.setUserAction(currentUser, request);
				return "redirect:/system/index.jsp";
			}
		}
	}
	
}
